Simplifying Recorded Scripts
As is often true of scriptable applications, the Finder allows you to express the same action in several different ways. Recorded scripts are precisely worded
so as to avoid ambiguity in variety of circumstances. In the script shown in Listing 1-1 on page 5, however, there is no need to specify the folder and
disk to which a window belongs. If you wish, you can specify a window by
name only:
set view of window "Letters" to sizeTo work correctly, the preceding statement must be contained in a Tell statement that names the Finder as the target application, and the window named Letters must be open. The container for a Finder window is always the Finder itself. If more than one window named Letters is open, the Finder changes the View property for all windows with that name tosize
.The previous section described how to add three lines at the end of the script shown in Listing 1-1 that set the window's views to different values. If instead you want all the windows opened by that script to have the same view, you could add just one line:
set view of windows to nameThe plural formwindows
identifies all open Finder windows.After opening a window, the recorded script in Listing 1-1 adjusts the window's Position and Size properties separately. The terms Position and Size name
two different properties of a window. Each property consists of a list of two integers. For the Position property, the two integers specify the coordinates of the upper-left corner of the content region of the window (the portion of the window that displays its contents; the title bar and scroll bars are not part of the content region). For the Size property, the two integers specify the vertical and horizontal dimensions of the window's content region.Instead of adjusting these properties separately, you can adjust them at the same time by setting the Bounds property. The value of the Bounds property is a list of four integers that specify the coordinates of the four corners of the window's content region. The first and second integers are identical to the value of the window's Position property. The third and fourth integers can be obtained by adding the first and second integers of the Position property to the first and second integers of the Size property, respectively.
You can easily obtain the bounds property of a window by asking the Finder for it. Just activate the window and run a script like this:
tell application "Finder" bounds of front window end tell --result: {4, 43, 373, 507}If you wish, you can rewrite a script like the one in Listing 1-1 using the Bounds property instead of Position and Size:
tell application "Finder" activate close every window open startup disk set bounds of window of startup disk to {4, 43, 373, 507} open folder "Financial" of startup disk set bounds of window "Financial" to {378, 43, 578, 198} open folder "Letters" of startup disk set bounds of window "Letters" to {379, 222, 579, 508} end tellAlthough this simplified version of the script in Listing 1-1 won't run appreciably faster than the original, it is easier to read. In a longer script,
using succinct statements can improve performance.